'use strict';

const Controller = require('egg').Controller;

class AmapController extends Controller {
    /**
     * 打开公共服务首页
     * GET
     */
    async showPoint() {
        const that = this
            , ctx = this.ctx
            , sid = ctx.query.sid // 指定景区
        let accName = ctx.query.acc;    
        let sortPosition = ctx.session.sortPoint;
        ctx.logger.info('>> showPoint()');   
        if (accName) {
            const saveAccName = ctx.session.accName;
            if (saveAccName !== accName) {
                ctx.session.accName = accName;
                ctx.session.serviceId = null;
                ctx.session.serviceType = null;
                ctx.logger.warn(ctx.session.serviceType)
            }
            ctx.cookies.set('acc', accName, {
                httpOnly: false,
                signed: false
            })
        }
        else {
            accName = ctx.session.accName || '';
        }

        if (sortPosition) {
            ctx.logger.info('>> from search');
            if (ctx.session.sortPoint) {
                sortPosition = ctx.session.sortPoint;
                ctx.logger.info(sortPosition);
            }  
        }

        // 服务景区列表参数
        ctx.state.httpOption = {
            url: ['public', 'listPublicServicePage'],
            data: {
                accName,
                currPage: 1,
                pageSize: 100
            }
        }
        // 获取服务景区列表数据
        await that.service.handleRequest.requestDate();
        const serverListPage = ctx.state.result.data;

        let serviceTypeSort = null; //服务类型排序
        let parkInfo = null;

        if (serverListPage.status === 200 && serverListPage.data.total > 0) {
            let serviceId = ctx.session.serviceId;
            if (!serviceId) serviceId = serverListPage.data.rows[0].id
            await that.getServiceTypeSort(serviceId);
            serviceTypeSort = ctx.state.result.data;          
        } else {
            ctx.session.serviceId = null;
        }

        await ctx.render('amap', {
            title: '公共服务',
            sid,
            serverListPage,
            serviceTypeSort,
            sortPosition,  // 搜索坐标
        }); 
    }
    /**
     * 获取各个公共服务类型坐标
     * POST
     */
    async getServiceTypePoints() {
        const { ctx } = this;
        const serviceType = ctx.query.serviceType || '';
        const serviceName = ctx.query.serviceName || '';
        const serviceId = ctx.session.serviceId;
        if (ctx.query.serviceType) ctx.session.serviceType = serviceType;
        ctx.state.httpOption = {
            url: ['public', 'detail', 'listPublicServiceDetailPage'],
            data: {
                serviceId,
                currPage: 1,
                pageSize: 100,
                serviceType,
                serviceName
            }
        }
        await this.service.handleRequest.requestDate();
        ctx.body = ctx.state.result.data;
    }
    /**
     * 重新获取底部目录数据
     * POST 
     */
    async resetPark() {
        const { ctx } = this;
        ctx.logger.info('>> resetPark()');
        const serviceId = ctx.query.serviceid;
        const isFirst = Number(ctx.query.isFirst);
        let footerMenuData  = null;
        ctx.session.sortPoint = null; // 去除搜索结果
        if (!isFirst) {
            ctx.logger.info('>> get getServiceTypeSort data');
            await this.getServiceTypeSort(serviceId);
            footerMenuData = ctx.state.result.data;
        }
        
        ctx.logger.info('>> get getParkInfo data');
        await this.getParkInfo(serviceId);
        const parkInfo = ctx.state.result.data;

        if (footerMenuData) {
            ctx.body = {         
                    footerMenuData,
                    parkInfo   
            };
        } else {
            ctx.body = {
                parkInfo
            };
        }   
            
    }
    /**
     * 获取公共服务菜单
     */
    async getServiceTypeSort(serviceId) {
        const { ctx } = this;
        ctx.session.serviceId = serviceId;
        ctx.state.httpOption = {
            url: ['public', 'detail', 'serviceTypeSort'],
            data: {
                serviceId: serviceId,
                currPage: 1,
                pageSize: 100
            }
        }

        await this.service.handleRequest.requestDate();
    }

    /**
     * 获取景区详情
     * @param {string} serviceId 景区服务id
     */
    async getParkInfo(serviceId){
        // serviceid 已经在获取服务菜单时存入cookie 不需要重复存储
        const {ctx} = this;
        ctx.state.httpOption = {
            url: ['public', 'map', 'publicServiceMapInfo'],
            data: {
                id:serviceId,
            }
        }

        await this.service.handleRequest.requestDate();
    }

    /**
     * 打开服务点详情页
     */
    async showInfo() {
        const { ctx } = this;
        ctx.logger.info('>> showInfo()');
        const id = ctx.params.id;
        ctx.logger.info('>> get publicServiceDetailInfo data');
        ctx.state.httpOption = {
            url: ['public', 'detail', 'publicServiceDetailInfo'],
            data: {
                id
            }
        }
        await this.service.handleRequest.requestDate();

        if (ctx.status === 200) {
            ctx.logger.info('>> render detail page');
            await ctx.render('detail', {
                title: '公共服务',
                info: ctx.state.result.data
            })
        }
    }
}

module.exports = AmapController;